package com.qc.exception;

import com.qc.dto.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 统一异常处理
 *
 * @author code
 * @version 1.0.0
 * @since 2022.06.20
 */
@RestControllerAdvice//使用该注解表示开启了全局异常的捕获
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public <T>Result<T>exceptionHandler(Exception e) {
        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            return Result.error(myException.getErrorCode(), myException.getErrorMsg());
        }

            return Result.error("500", "服务器端异常");

    }
}